<?php
$conn = mysqli_connect('localhost' , 'root' , 'weicedbmojito160824','weixin_didao_wxapp');
$query = "Set Names 'utf8mb4'";
mysqli_query($conn,$query);
/**
 * Flag为1 是attend
 * Flag为0 是cancel
 */
if($_GET['flag'] == '1'){
    if(isset($_GET['attenderNum']) && isset($_GET['remindPicker']) && isset($_GET['partyId']) && isset($_GET['maxMember']) &&  isset($_GET['flag']) && $_GET['jyj']==='jyj') {
        $partyId = $_GET['partyId'];
        $formId = $_GET['formId'];
        $maxMember = $_GET['maxMember'];
        $attenderNum = $_GET['attenderNum'];
        $remindPicker = $_GET['remindPicker'];
        isset($_GET['description']) ? $_GET['description'] != 'undefined' ? $description = $_GET['description'] : $description = '' : $description = '';
        $userInfo = $_GET['userInfo'];
        $openid = json_decode($userInfo, true)['openId'];

        $query = " SELECT SUM(attender_num) AS attender_num_sum  FROM didao_wxapp_party_attender WHERE party_id = '$partyId'  AND cancel_flag = 1";
        $attenderNumSumResult = mysqli_query($conn, $query);
        $attenderNumSum = mysqli_fetch_assoc($attenderNumSumResult)['attender_num_sum'];

        $query = "SELECT `openid` FROM didao_wxapp_party_attender WHERE party_id = '$partyId' AND openid = '$openid' ";
        $result = mysqli_query($conn, $query);

        if (mysqli_num_rows($result) == 0) {

            if ($attenderNum <= ($maxMember - $attenderNumSum)) {
                $num = $attenderNumSum + $attenderNum;
                $query = "UPDATE didao_wxapp_party SET attender_num = '$num' WHERE id = '$partyId'";
                $result = mysqli_query($conn, $query);
                if ($result) {
                    $query = "INSERT INTO didao_wxapp_party_attender (`party_id`,`form_id`,`is_remind`,`openid`,`remind_picker`,`attender_num`,`create_time`,`update_time`,`cancel_flag`)
                  VALUES ('$partyId','$formId','0','$openid','$remindPicker','$attenderNum',NOW(),NOW(),1)";

                    $result = mysqli_query($conn, $query);
                    echo json_encode(array(
                        'errCode' => 1,
                        'errMsg' => '参与成功',
                        'partyId' => $partyId
                    ), JSON_UNESCAPED_UNICODE);
                } else {
                    echo json_encode(array(
                        'errCode' => -1,
                        'errMsg' => '网络错误',
                        'partyId' => -1
                    ), JSON_UNESCAPED_UNICODE);
                }
            }
            else {
                echo json_encode(array(
                    'errCode' => -1,
                    'errMsg' => '参与人数超过活动人数,还剩' . ($maxMember - $attenderNumSum) . '个位置',
                    'partyId' => -1
                ), JSON_UNESCAPED_UNICODE);
            }
        } else {
            if ($attenderNum <= ($maxMember - $attenderNumSum)) {
                $num = $attenderNumSum + $attenderNum;
                $query = "UPDATE didao_wxapp_party SET attender_num = '$num' WHERE id = '$partyId'";
                $result = mysqli_query($conn, $query);
                if ($result) {
                    $query = "UPDATE didao_wxapp_party_attender 
SET `party_id`='$partyId',`form_id`='$formId',`is_remind`='0',`openid`='$openid',`remind_picker`='$remindPicker',`attender_num`='$attenderNum',`update_time`=NOW(),`cancel_flag`=1
                              WHERE openid = '$openid' AND party_id = '$partyId'";

                    $result = mysqli_query($conn, $query);
                    echo json_encode(array(
                        'errCode' => 1,
                        'errMsg' => '更新成功',
                        'partyId' => $partyId
                    ), JSON_UNESCAPED_UNICODE);
                } else {
                    echo json_encode(array(
                        'errCode' => -1,
                        'errMsg' => '网络错误',
                        'partyId' => -1
                    ), JSON_UNESCAPED_UNICODE);
                }
            }
            else {
                echo json_encode(array(
                    'errCode' => -1,
                    'errMsg' => '参与人数超过活动人数,还剩' . ($maxMember - $attenderNumSum) . '个位置',
                    'partyId' => -1
                ), JSON_UNESCAPED_UNICODE);
            }
        }
    }
}
elseif($_GET['flag'] == 0){
    if(isset($_GET['partyId']) && isset($_GET['openid']) && $_GET['jyj']==='jyj') {
        $openid = $_GET['openid'];
        $formId = $_GET['formId'];
        $partyId = $_GET['partyId'];
        $query = "UPDATE didao_wxapp_party_attender SET cancel_flag = '-1',update_time=NOW(),form_id='$formId' WHERE  party_id = '$partyId' AND openid = '$openid'";
        $result = mysqli_query($conn , $query);
        if ($result){
            echo json_encode(array(
                'errCode' => 1,
                'errMsg'=> '取消成功',
                'partyId' => $partyId
            ),JSON_UNESCAPED_UNICODE);
        }else{
            echo json_encode(array(
                'errCode' => -1,
                'errMsg'=>'网络出错',
                'partyId' => -1
            ),JSON_UNESCAPED_UNICODE);
        }
    }
}
else{
    echo json_encode(array(
        'errCode' => -1,
        'errMsg'=>'输入有误',
        'partyId' => -1
    ),JSON_UNESCAPED_UNICODE);
}
/**
 * Created by PhpStorm.
 * User: a7849
 * Date: 2017/7/18
 * Time: 11:26
 */